package com.nclg.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nclg.exam.dto.*;
import com.nclg.exam.entity.TPaper;
import com.nclg.exam.mapper.PaperMapper;
import com.nclg.exam.mapper.QuestionMapper;
import com.nclg.exam.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PaperServiceImpl implements PaperService {

    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询出卷人
     * @return
     */
    @Override
    public List<CreaterDto> selectCreaters() {
        return paperMapper.selectCreaters();
    }

    /**
     * 查询试卷
     * @param dto
     * @return
     */
    @Override
    public PageInfo<PaperListDto> selectByPage(PaperDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<PaperListDto> list = paperMapper.selectByPage(dto);
        return new PageInfo<>(list);
    }

    /**
     * 修改试卷状态
     * @param pstatus
     * @param pId
     * @return
     */
    @Override
    public Boolean updatePaperStatus(String pstatus, Long pId) {
        return paperMapper.updatePaperStatus(pstatus, pId) > 0 ? true : false;
    }

    /**
     * 查询试卷信息
     * @param pId
     * @return
     */
    @Override
    public UpdatePaperDto selectById(Long pId) {
        return paperMapper.selectById(pId);
    }

    /**
     * 编辑试卷考试时间
     * @param dto
     * @return
     */
    @Override
    public Boolean updatePaper(UpdatePaperDto dto) {
        return paperMapper.updatePaper(dto) > 0 ? true : false;
    }

    /**
     * 查询该类型题目总数量
     * @param type
     * @return
     */
    @Override
    public int selectQuestionCount(String type) {
        return paperMapper.selectQuestionCount(type);
    }

    /**
     * 创建试卷
     * @param dto
     * @return
     */
    @Override
    public boolean insertPaper(InsertPaperDto dto) {
        // 生成单选题 ids..
        List<QuestionIdAndScoreDto> singles = questionMapper.selectQuestionByRandom("1", dto.getQSingle());
        // 生成多选题 ids..
        List<QuestionIdAndScoreDto> multiples = questionMapper.selectQuestionByRandom("2", dto.getQMultiple());
        // 生成简答题 ids..
        List<QuestionIdAndScoreDto> shorts = questionMapper.selectQuestionByRandom("3", dto.getQSingle());

        BigDecimal score = BigDecimal.ZERO;
        List<Long> ids = new ArrayList<>();

        if (null != singles && !singles.isEmpty()){
            for (QuestionIdAndScoreDto single : singles) {
                ids.add(single.getQId());
                score = score.add(single.getQScore());
            }
        }
        if (null != multiples && !multiples.isEmpty()){
            for (QuestionIdAndScoreDto multiple : multiples) {
                ids.add(multiple.getQId());
                score = score.add(multiple.getQScore());
            }
        }
        if (null != shorts && !shorts.isEmpty()){
            for (QuestionIdAndScoreDto s : shorts) {
                ids.add(s.getQId());
                score = score.add(s.getQScore());
            }
        }
        TPaper paper = new TPaper();
        paper.setPName(dto.getPName());
        paper.setPCreatetime(new Date());
        paper.setPBegintime(dto.getPBegintime());
        paper.setPEndtime(dto.getPEndtime());
        paper.setPDuration(dto.getPDuration());
        paper.setPScore(score);
        paper.setUId(dto.getUId());
        if (ids.isEmpty()) return false;
        String s = StringUtils.join(ids, ",");
        paper.setPQuestionids(s);
        return paperMapper.insertPaper(paper) > 0 ? true : false;
    }

    /**
     * 判断pId是否为正常且正在考试的试卷
     * @param pId
     * @return
     */
    @Override
    public boolean selectIsExamingById(Long pId) {
        return paperMapper.selectIsExamingById(pId) > 0 ? true : false;
    }
}
